<template>
  <!-- 在 vue 这种单页面的应用程序中，
    它只存在一个页面 - App.vue，其他的都是组件 -->
  <div class="main">
    <!-- <router-view/> -->
    <keep-alive :include="currentComponent">
      <!-- 动态组件 -->
      <component :is="currentComponent"></component>
    </keep-alive>
    <van-tabbar v-model="currentComponent">
      <van-tabbar-item name="home" icon="home-o">首页</van-tabbar-item>
      <!-- <van-tabbar-item name="message" icon="sign">智能</van-tabbar-item> -->
      <!-- <van-tabbar-item name="message" icon="friends-o">消息</van-tabbar-item> -->
      <van-tabbar-item name="mine" icon="setting-o">设置</van-tabbar-item>
    </van-tabbar>
    
  </div>
</template>

<script>
import { Dialog } from "vant";
import { Toast } from "vant";
import home from "@c/ToolBar/Home"
import {queryUserDetailById} from "@/api/api.js"
export default {
  name: "Main",
  components: {
    //'tool-bar': toolBar,
    // 异步组件引入方式, 异步组件：只有在需要去展示这个组件的时候，才会把组件去进行渲染。
    home,
    // 'take': () => import('@c/Take'),
    // 'add': () => import('@c/Add'),
    message: () => import("@c/ToolBar/Message"),
    mine: () => import("@c/ToolBar/Mine"),
  },
  created: function () {
    this.$http
      .get("http://121.5.58.100:8000/queryOTA", {
        params: {
          userAgent: navigator.userAgent,
          version: this.$version,
        },
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
      .then((result) => {
        if (result.data.version > this.$version) {
          console.log(result);
          Dialog.confirm({
            title: "发现新版本 V" + result.data.version,
            confirmButtonText: "更新",
            message: result.data.content,
          })
            .then(() => {
              var self = this;
              // on confirm
              // window.location.href =
              //   "http://121.5.58.100/base-" + result.data.version + ".apk";

              var fileTransfer = new window.FileTransfer();
              // 上传进度
              fileTransfer.onprogress = function (progressEvent) {
                if (progressEvent.lengthComputable) {
                  self.progress = (
                    (progressEvent.loaded / progressEvent.total) *
                    100
                  ).toFixed(0);
                  Toast({
                    message: "下载中：" + self.progress + "%",
                    position: "bottom",
                  });
                } else {
                  this.progress = 0;
                  // loadingStatus.increment();
                }
              };
              var uri = encodeURI(
                "http://121.5.58.100/base-" + result.data.version + ".apk"
              );
              var fileURL = "cdvfile://localhost/temporary/tuner.apk";
              fileTransfer.download(
                uri,
                fileURL,
                function (entry) {
                  alert("下载完成，点击确定开始安装" + entry.toURL());
                  cordova.plugins.fileOpener2.open(
                    "cdvfile://localhost/temporary/tuner.apk",
                    "application/vnd.android.package-archive"
                  );
                },
                function (error) {
                  alert(
                    "download error: " +
                      error.source +
                      error.target +
                      error.code
                  );
                },
                false,
                {
                  headers: {
                    Authorization: "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==",
                  },
                }
              );
            })
            .catch(() => {
              // on cancel
            });
        }
      });
    queryUserDetailById({
      userID:localStorage.getItem("setUserID")
      })
    .then((result)=>{
      console.log(result)
      this.$store.commit("saveNickName", result.username);
    })
    // .then(() => {
    //   // on confirm
    //   // window.location.href =
    //   //   "http://121.5.58.100/base-" + result.data.version + ".apk";

    //   var fileTransfer = new window.FileTransfer();
    //   // 上传进度
    //   fileTransfer.onprogress = function (progressEvent) {
    //     if (progressEvent.lengthComputable) {
    //       this.progress = (
    //         (progressEvent.loaded / progressEvent.total) *
    //         100
    //       ).toFixed(0);
    //       Toast({
    //         message: "下载中：" + this.progress + "%",
    //         position: "bottom",
    //       });
    //     } else {
    //       this.progress = 0;
    //       // loadingStatus.increment();
    //     }
    //   };
    //   var uri = encodeURI(
    //     "http://121.5.58.100/base-" + result.data.version + ".apk"
    //   );
    //   var fileURL = "cdvfile://localhost/temporary/tuner.apk";
    //   fileTransfer.download(
    //     uri,
    //     fileURL,
    //     function (entry) {
    //       alert("下载完成，点击确定开始安装" + entry.toURL());
    //       cordova.plugins.fileOpener2.open(
    //         "cdvfile://localhost/temporary/tuner.apk",
    //         "application/vnd.android.package-archive"
    //       );
    //     },
    //     function (error) {
    //       alert(
    //         "download error: " +
    //           error.source +
    //           error.target +
    //           error.code
    //       );
    //     },
    //     false,
    //     {
    //       headers: {
    //         Authorization: "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==",
    //       },
    //     }
    //   );
    // })
    // .catch(() => {
    //   // on cancel
    // });
  },
  data: function () {
    return {
      progress:0,
      currentComponent: "home",
    };
  },
  activated: function () {
    console.log(this.currentComponent);
    //console.log(this.$route.params.value)
    // 在 keepAlive 被激活的时候，调用指定加载页面组件的方法
    // this.LoginCheck();
    // console.log("keepok");
    // console.log(this.$store.state.username)
  },
  methods: {},
};
</script>

<style lang="scss" scoped>
.main {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  color: #342343;
}
</style>
